package com.s7.recycler.helper;

import android.support.v7.widget.RecyclerView;

import com.s7.recycler.adapter.BaseRecyclerAdapter;
import com.s7.recycler.listener.Recycler;

import java.util.List;

/**
 * Created by Administor on 2018\7\6 0006.
 * RecyclerView普通工具--无功能
 * 兼容Grid布局、Linear布局、StaggeredGrid布局（支持布局切换）
 */
public class RecyclerHelper<T> {

    /**
     * adapter 存在的标志
     */
    private boolean hasInitAdapter = false;

    private BaseRecyclerAdapter<T> mAdapter;
    private RecyclerView mRecycler;

    private Recycler.OnItemClickListener<T> mItemClick;
    private Recycler.OnCheckedChangeListener<T> mCheckedChange;
    private Recycler.OnClickListener<T> mClick;
    private Recycler.OnItemLongClickListener<T> mItemLongClick;

    /**
     * 设置Adapter
     * @param mAdapter
     */
    protected void setAdapter(BaseRecyclerAdapter<T> mAdapter) {
        this.mAdapter = mAdapter;
    }

    /**
     * 设置RecyclerView
     * @param mRecycler
     */
    protected void setRecycler(RecyclerView mRecycler) {
        this.mRecycler = mRecycler;
    }

    public void setCheckedChange(Recycler.OnCheckedChangeListener<T> mCheckedChange) {
        this.mCheckedChange = mCheckedChange;
    }

    protected void setClick(Recycler.OnClickListener<T> mClick) {
        this.mClick = mClick;
    }

    protected void setItemClick(Recycler.OnItemClickListener<T> mItemClick) {
        this.mItemClick = mItemClick;
    }

    protected void setItemLongClick(Recycler.OnItemLongClickListener<T> mItemLongClick) {
        this.mItemLongClick = mItemLongClick;
    }

    protected void initAdapter(){

        if (mAdapter == null || mRecycler == null) {
            return;
        }

        mAdapter.setCheckedChange(mCheckedChange)
                .setClick(mClick)
                .setItemClick(mItemClick)
                .setItemLongClick(mItemLongClick);
        mRecycler.setAdapter(mAdapter);
        hasInitAdapter = true;
    }

    /**
     * 初始化
     */
    public void init() {
        if(!hasInitAdapter){
            initAdapter();
        }
    }

    /**
     * 切换RecyclerView 布局管理
     * @param layoutManager
     */
    public void setLayoutManager(RecyclerView.LayoutManager layoutManager) {
        if (mRecycler != null && mAdapter != null) {
            mRecycler.setLayoutManager(layoutManager);
            notifyAdapter();
        }
    }

    /**
     * 刷新Adapter
     * @param datas
     */
    public void notifyAdapter(List<T> datas) {
        init();
        mAdapter.onRefresh(datas);
    }

    /**
     * 刷新Adapter
     */
    public void notifyAdapter() {
        init();
        mAdapter.notifyDataSetChanged();
    }

    /**
     * 替换指定位置数据
     * @param position
     * @param data
     */
    public void notifyReplace(int position, T data) {
        mAdapter.onReplace(position, data);
    }

    /**
     * 删除指定位置的内容
     * @param position
     */
    public void notifyRemove(int position) {
        mAdapter.onRemove(position);
    }

    /**
     * 获取所有数据
     * @return
     */
    public List<T> getData() {
        return mAdapter.getDatas();
    }

    public static class Builder<T> {
        private BaseRecyclerAdapter<T> mAdapter;
        private RecyclerView mRecycler;
        private Recycler.OnCheckedChangeListener<T> mCheckedChange;
        private Recycler.OnClickListener<T> mClick;
        private Recycler.OnItemClickListener<T> mItemClick;
        private Recycler.OnItemLongClickListener<T> mItemLongClick;

        public Builder setAdapter(BaseRecyclerAdapter<T> mAdapter) {
            this.mAdapter = mAdapter;
            return this;
        }

        public Builder setRecycler(RecyclerView mRecycler, RecyclerView.LayoutManager layoutManager) {
            this.mRecycler = mRecycler;
            this.mRecycler.setLayoutManager(layoutManager);
            return this;
        }

        public Builder setRecycler(RecyclerView mRecycler, RecyclerView.LayoutManager layoutManager, RecyclerView.ItemDecoration itemDecoration) {
            this.mRecycler = mRecycler;
            this.mRecycler.setLayoutManager(layoutManager);
            this.mRecycler.addItemDecoration(itemDecoration);
            return this;
        }

         public Builder setCheckedChange(Recycler.OnCheckedChangeListener<T> mCheckedChange) {
            this.mCheckedChange = mCheckedChange;
            return this;
        }

        public Builder setClick(Recycler.OnClickListener<T> mClick) {
            this.mClick = mClick;
            return this;
        }

        public Builder setItemClick(Recycler.OnItemClickListener<T> mItemClick) {
            this.mItemClick = mItemClick;
            return this;
        }

        public Builder setItemLongClick(Recycler.OnItemLongClickListener<T> mItemLongClick) {
            this.mItemLongClick = mItemLongClick;
            return this;
        }


        public <T> RecyclerHelper<T> build() {
            RecyclerHelper mHelper = new RecyclerHelper<T>();
            mHelper.setAdapter(mAdapter);
            mHelper.setRecycler(mRecycler);
            mHelper.setItemClick(mItemClick);
            mHelper.setCheckedChange(mCheckedChange);
            mHelper.setClick(mClick);
            mHelper.setItemClick(mItemClick);
            mHelper.setItemLongClick(mItemLongClick);
            return mHelper;
        }
    }
}
